﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace ConsoleApp
{
    public class IndexerTest2
    {
        public void Show()
        {
            PersonList person = new PersonList();
            person.Add(new Person() { Name = "李俊辉",Password="123456" });
            person.Add(new Person() { Name = "李俊辉",Password="abcdef" });
            person.Add(new Person() { Name = "test",Password="abcdef" });

            var person1 = person["李俊辉"];
            var person2 = person["test"];
            var person3 = person[1];
            var person4 = person[0];

            person.Add(new Person() { Name = "test", Password = "abcdef" });
            person.Add(new Person() { Name = "test", Password = "abcdef" });
        }

        
        class Person
        {
            /// <summary>
            /// 姓名（不一定唯一），["姓名"]时，如果相同，取第一个
            /// </summary>
            public string Name { get; set; }

            public string Password { get; set; }
        }

        class PersonList
        {
            protected Person[] _peoples;   //集合
            protected int _pointer;       //指针
            public PersonList()
            {
                _peoples = new Person[4];
                _pointer = 0;
            }

            /// <summary>
            /// 可变长度
            /// </summary>
            private void CheckSetLeng()
            {
                var temps = _peoples;
                _peoples = new Person[_pointer * 2];
                for (int i = 0; i < temps.Length; i++)
                {
                    _peoples[i] = temps[i];
                }

            }

            /// <summary>
            /// 添加
            /// </summary>
            /// <param name="person"></param>
            public void Add(Person person)
            {
                if (_peoples.Length <= _pointer)
                    CheckSetLeng();

                _peoples[_pointer] = person;
                _pointer++;
            }

            /// <summary>
            /// 删除
            /// </summary>
            /// <param name="index"></param>
            public void Remove(int index)
            {
                if (index < 0 || index >= _pointer)
                {
                    throw new Exception("index 无效");
                }
                _peoples[index] = null;
                _pointer--;
            }

            /// <summary>
            /// 集合长度
            /// </summary>
            public int Length { get => _pointer; }

            
            //定义索引器，name 字段的索引值为 0 ，password 字段的索引值为 1
            public Person this[int index]
            {
                get
                {
                    if (index < 0 || index >= _pointer)
                        throw new Exception("index 无效");
                    return _peoples[index];
                }
                set
                {
                    if (index < 0 || index >= _pointer)
                        throw new Exception("index 无效");
                    _peoples[index] = value;
                }
            }

            public Person this[string key]
            {
                get
                {
                    foreach (var item in _peoples)
                    {
                        if (item.Name== key)
                        {
                            return item;
                        }
                    }
                    return null;
                }
                set
                {
                    for (int i = 0; i < Length; i++)
                    {
                        if (_peoples[i].Name == key)
                        {
                            _peoples[i] = value;
                        }
                    }
                    throw new Exception("key 无效");
                }
            }

        }

    }
}
